package com.example.dllo.my36kr.welcome;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.main.MainActivity;
import com.example.dllo.my36kr.tools.BaseAty;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by dllo on 16/9/1.
 */
public class WelcomeAty extends BaseAty {
    private CountDownTimer timer;
    private ImageView layout;
    private Button welcomeBnt;

    @Override
    protected int setLayout() {
        return R.layout.aty_welcome;
    }

    @Override
    protected void initView() {
        layout = (ImageView) findViewById(R.id.iv_aty_welcome);
        welcomeBnt = bindView(R.id.bnt_aty_welcome);
        welcomeBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
                Intent intent = new Intent(WelcomeAty.this, MainActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void initData() {
        WelcomeAsyncTask task = new WelcomeAsyncTask();
        task.execute("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1473161423999&di=6dbe34f2f28d47569728e0391dac39bc&imgtype=jpg&src=http%3A%2F%2Ftupian.enterdesk.com%2Fuploadfile%2F2014%2F1122%2F20141122101258256.jpg");
        timer = new CountDownTimer(5000 , 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                String time = millisUntilFinished / 1000 + "s";
                welcomeBnt.setText(time);
            }
            @Override
            public void onFinish() {
                Intent intent = new Intent(WelcomeAty.this, MainActivity.class);
                startActivity(intent);
                finish();

            }
        }.start();
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.image_anim);
        layout.startAnimation(animation);
    }

    private class WelcomeAsyncTask extends AsyncTask<String , Void ,Bitmap> {
        private Bitmap bitmap;
        @Override
        protected Bitmap doInBackground(String... params) {
            try {
                URL url = new URL(params[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK){
                    InputStream is = connection.getInputStream();
                    bitmap = BitmapFactory.decodeStream(is);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bitmap;
        }
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            layout.setBackgroundDrawable(new BitmapDrawable(bitmap));
        }
    }
}
